package com.ljcalc.business.components;

import java.util.LinkedList;
import java.util.List;

import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;
import org.apache.xerces.xni.XMLString;

import com.ljcalc.business.components.fetchers.HistoryResultItemListHandler;


/**
 * record the content after the occourances of class=mgb, in the first td tag of the first table after mgb
 * @author SELuoc
 *
 */
public class SSLHistoryResultItemListHandler extends HistoryResultItemListHandler {
	
	private int tableCounter = 0;
	private int tdCounter = 0;
	private boolean lastIsSpan = false;

	
	public SSLHistoryResultItemListHandler(){
		super();
		tableCounter = 0;
		tdCounter = 0;
		lastIsSpan = false;
	}
	
	public void startElement(QName element, XMLAttributes attrs,
			Augmentations augs) {
		String clazz = attrs.getValue("class");
		if(!startRecording && clazz!=null && clazz.equalsIgnoreCase("mgb")){
			tableCounter = 0;
			tdCounter = 0;
			startRecording = true;
		}
		
		if(startRecording){
			if(element.rawname.equalsIgnoreCase("TABLE")){
				tableCounter++;
			}
			if(element.rawname.equalsIgnoreCase("TD")){
				tdCounter++;
			}
		}
		
		if(tableCounter>1||tdCounter>1){
			startRecording= false;
			tableCounter = 0;
			tdCounter = 0;
		}
		
	}

	public void endElement(QName element, Augmentations augs) {
		if(startRecording&&tableCounter==1&&tdCounter==1){
			lastIsSpan = element.rawname.equalsIgnoreCase("SPAN");
		}

    }
	
    /** Characters. */
    public void characters(XMLString text, Augmentations augs){
    	if(startRecording&&tableCounter==1&&tdCounter==1&&lastIsSpan){
    		
    		if(comb==null||comb.isFull()){
    			comb = new SSLCombination();
    		}
    		
    		list = extract(list, comb, text);
    	}

    } // characters(XMLString,Augmentations)





	
}
